from scrapy.spider import BaseSpider
from scrapy.selector import Selector

from spider.items import SpiderItem
import MySQLdb


class CarshopSpider(BaseSpider):

    name = "carshop"
    baseurl ="http://dealer.autohome.com.cn/china/"
    lis = []
    for i in range(1,1375):
        lis.append(baseurl+"0_0_0_0_"+str(i)+".html")

    #start_urls = ["http://dealer.autohome.com.cn/china/"]
    #start_urls = ["http://localhost190/a.html"] 
    start_urls = lis 


    def parse(self,response):
     
        sel = Selector(response)
        item = SpiderItem()   
        
        area = sel.xpath('//div[@class="tab-content"]//div[@class="dealer-cont  js-dealer"] | //div[@class="dealer-cont border-b-no js-dealer"]')

        item["carshop"] = area.xpath('.//h3[1]//a[2]/text()').extract()
        item["carshopurl"] = area.xpath('.//h3[1]//a[2]/@href').extract()
        item["carshopb"] = area.xpath('.//h3[1]//a[2]/@js-dbrand').extract()
        item["carshopd"] = area.xpath('.//h3[1]//a[2]/@js-darea').extract()
        item["tel"] = area.xpath('.//dl//span[@class="dealer-api-phone"]/text() | .//dl//span[@class="dealer-api-phone black"]/text()').extract()
        item["address"] = area.xpath('.//dl//dd[@class="c666"]//div[3]/@title').extract()
        item["type"] = "carshop"

        return item
